/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.io;

import wtkx.Scene;

import jbxml.Dictionary;
import jbxml.Resolver;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;

/**
 * Read a simplified CSS syntax.  Named values.  Wants to do full CSS as in 
 * http://www.w3.org/TR/css-style-attr
 * 
 * 
 * @see jbxml.Value
 * @author jdp
 */
public class CSS 
    extends jbxml.Resolver
{
    public final static Charset UTF8 = Charset.forName("UTF-8");

    private final static Class ClassNil = null;


    public CSS(Scene scene, Dictionary<String, ?> resources) {
        super(scene,resources);
    }
    public CSS(Resolver parent) {
        super(parent);
    }


    public Object readObject(InputStream inputStream)
        throws IOException
    {
        return this.parse(new InputStreamReader(inputStream, UTF8));
    }
    public Dictionary<String,?> parse(String string)
        throws IOException
    {
        return this.parse(new StringReader(string));
    }

    public Dictionary<String,?> parse(Reader reader)
        throws IOException
    {
        this.enter();
        try {
            Dictionary<String,Object> map = new Dictionary.Utils<String,Object>();

            while (true){

                String name = ReadName(reader);
                if (null != name){
                    String value = ReadValue(reader);
                    if (null != value){

                        try {
                            Object ovalue = this.resolve(value,ClassNil);

                            if (null != ovalue)

                                map.put(name,ovalue);
                        }
                        catch (Exception exc){
                        }
                    }
                    else
                        return map;
                }
                else
                    return map;
            }
        }
        finally {
            this.exit();
        }
    }

    private final static String ReadName(Reader r)
        throws IOException
    {
        int ch = SkipWhitespace(r);
        if (-1 == ch)
            return null;
        else {
            StringBuilder string = new StringBuilder();
            string.append( (char)ch);
            inl:
            while (true){
                switch (ch = r.read()){
                case -1:
                    return null;
                case '{':
                    if (0 < string.length())
                        throw new IllegalStateException("Named '{' groups '}' not supported (reading at '"+string+"').");
                    break;
                case '}':
                    throw new IllegalStateException("Encountered misplaced '{' while reading name '"+string+"'.");
                case ':':
                    return string.toString();
                default:
                    string.append( (char)ch);
                }
            }
        }
    }
    private final static String ReadValue(Reader r)
        throws IOException
    {
        int ch = SkipWhitespace(r);
        if (-1 == ch)
            return null;
        else {
            StringBuilder string = new StringBuilder();
            string.append( (char)ch);
            inl:
            while (true){
                switch (ch = r.read()){
                case -1:
                    return string.toString();
                case '{':
                case '}':
                case ';':
                    return string.toString();
                default:
                    string.append( (char)ch);
                }
            }
        }
    }
    private final static int SkipWhitespace(Reader r)
        throws IOException
    {
        int ch = r.read();

        while (ch != -1 && Character.isWhitespace(ch)) {
            ch = r.read();
        }
        return ch;
    }

}
